package pandora.data.mysql;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pandora.connection.ConnectionManager;
import pandora.data.ITypeDAO;
import pandora.dto.TypeDTO;

public class JDBCTypeDAO implements ITypeDAO {

	private Connection conn;
	
	public JDBCTypeDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}

    protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }
    
	public void delete(Connection conn, String typeOID) {
		// TODO Auto-generated method stub
		
	}

	public void insert(Connection conn, String oidt, int typeCode) {
		// TODO Auto-generated method stub
		
	}

	public void insert(Connection conn, String oidt, String typeCode) {
		// TODO Auto-generated method stub
		
	}

	
	public void insert(Connection conn, String oidt, TypeDTO typeDTO) {
		// TODO Auto-generated method stub
		
	}


	
	public TypeDTO select(Connection conn, String oidt) {
		// TODO Auto-generated method stub
		return null;
	}


	//DCC
	public TypeDTO selectTypeByOID(String typeOID) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		TypeDTO type = null;
		
		String sql = "SELECT T.* FROM Types T WHERE (T.OID = ?) ";
		
		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, typeOID);
			
			result = stmt.executeQuery();
			
			if (result.next()) {
				
				type = new TypeDTO();
				
				if(result.getString("OID").equals(result.getString("parentOID"))){
					type.setParentType(null);
					type.setType(result.getString("type"));
				} else{
					type.setParentType(selectTypeByOID(result.getString("parentOID")));
					type.setType(result.getString("type"));
				}
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return type;
	}
	
	public String selectTypeOIDByTypeName(TypeDTO t){
		 PreparedStatement stmt = null;
		 ResultSet result = null;
		 String oid = null;
		 String sql = "SELECT T.* FROM Types T WHERE (T.type = ?) ";

	     try {
	        stmt = conn.prepareStatement(sql);
	        	
	        stmt.setString(1, t.getType());
	        result = stmt.executeQuery();

	        if (result.next()) {
	            oid = result.getString("OID");
	        }
	        } catch (SQLException e) {
	        	System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oid;
	}
	//Fin DCC
}